package com.shuiyun.service.impl;

import com.shuiyun.dao.GoodsMapper;
import com.shuiyun.dao.PortMapper;
import com.shuiyun.dao.UserMapper;
import com.shuiyun.entity.Goods;
import com.shuiyun.service.GoodsService;
import com.shuiyun.utils.DataGridResult;
import com.shuiyun.utils.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService{

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private PortMapper portMapper;

    @Resource
    private UserMapper userMapper;
    @Override
    public DataGridResult findAllGoods(Query query) {
        List<Goods> list = goodsMapper.findAllGoodsByPage(query);
        System.out.println(list);
        long total = goodsMapper.countGoods();
        DataGridResult result = new DataGridResult(list,Integer.parseInt(total+""));
        return result;
    }

    //通过id查找货物
    @Override
    public Goods findGoodsById(Integer goodsId) {

        Goods goods = goodsMapper.findGoodsById(goodsId);
        return goods;
    }

    //更新货物信息
    @Override
    public int updateGoods(Goods goods) {

        //根据港口名 修改港口id
        Integer startId = portMapper.findPortByName(goods.getStartPort().getPortName());
        Integer endId = portMapper.findPortByName(goods.getEndPort().getPortName());
        goods.setGoodsStartPortId(startId);
        goods.setGoodsEndPortId(endId);

        //根据用户名 修改用户id
        Integer userId = userMapper.findUserIdByName(goods.getUser().getUserName());
        goods.setUserId(userId);
        return goodsMapper.updateGoods(goods);
    }

    //批量删除
    @Override
    public int deleteGoods(Integer[] ids) {

        return goodsMapper.deleteBatch(ids);
    }

    //增加货物
    @Override
    public int addGoods(Goods goods) {
        return goodsMapper.insertSelective(goods);
    }
}
